#include <stdio.h>
#include <stdlib.h>
#include "SetOperation.c"

void nuionSet(LinkList la,LinkList lb,LinkList* lc);//*********求并集函数
void occurSet(LinkList la,LinkList lb,LinkList* lc);//*********求交集函数

int main(){

	LinkList la,lb,lc;
	char a[9]="magazine";
	char b[]="mznei";

	InitList(&la,a,8);
	InitList(&lb,b,5);
	InitList(&lc,a,0);
	char* str;
	ToString(la,str);
	printf("%s\n",str);
	ToString(lb,str);
	printf("%s\n",str);
	//nuionSet(la,lb,&lc);
	occurSet(la,lb,&lc);
	ToString(lc,str);
	printf("%s\n",str);

	DestoryList(la);
	DestoryList(lb);
	DestoryList(lc);
}

void nuionSet(LinkList la,LinkList lb,LinkList* lc){
	merger(la,lb,lc);
	sort(*lc,0);
	ElemSet next,cur,e;
	int i=0,len=ListLength(*lc);
	while(i++<len){
		GetElem(*lc,i,&cur);
		GetElem(*lc,i+1,&next);
		while(cur==next && i+1<len--){
			ListDelete(*lc,i+1,&e);
			GetElem(*lc,i+1,&next);
		}
	}
}

void occurSet(LinkList la,LinkList lb,LinkList* lc){
	int a=ListLength(la);
	int b=ListLength(lb);
	sort(la,1);
	sort(lb,2);
	int i=0,j=0,index=1;
	ElemSet am,bm,e;

	while(i++<a){
		GetElem(la,i,&am);
		j=0;
		while(j++ < b){
			GetElem(lb,j,&bm);
			if(am==bm){
				ListInsert(*lc,index++,am);
				ListDelete(lb,j--,&e);
				b--;
			}
		}
	}
	sort(*lc,1);
}